/**
 * 
 */
package com.ez.supervise.service.impl;

import java.util.List;

import com.ez.supervise.core.Page;
import com.ez.supervise.dao.GoalCategoryDao;
import com.ez.supervise.dao.GoalDetailDao;
import com.ez.supervise.model.biz.GoalCategory;
import com.ez.supervise.model.biz.GoalDetail;
import com.ez.supervise.service.GoalDetailService;

/**
 * @author searph.wang
 * @email searph.wang@gmail.com
 * @Created at 2009-7-22
 * 
 */
public class GoalDetailServiceImpl implements GoalDetailService {
	private GoalCategoryDao goalCategoryDao;
	private GoalDetailDao goalDetailDao;

	// public int update(GoalDetail org) {
	// return sqlMapClientTemplate.update("sys.updateGoalDetail", org);
	// }
	public void setGoalCategoryDao(GoalCategoryDao goalCategoryDao) {
		this.goalCategoryDao = goalCategoryDao;
	}

	public void setGoalDetailDao(GoalDetailDao goalDetailDao) {
		this.goalDetailDao = goalDetailDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#delete(java.lang.Long)
	 */
	public void delete(Long id) {
		goalDetailDao.delete(id);
		GoalCategory category = (GoalCategory) goalCategoryDao.get(id);
		if (category != null && category.isLeaf()) {
			goalCategoryDao.updatePercent(category.getId());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#insert(java.lang.Object)
	 */
	public void insert(Object entity) {
		GoalDetail goalDetail = (GoalDetail) entity;
		GoalCategory category = (GoalCategory) goalCategoryDao.get(goalDetail
				.getCategoryId());
		goalDetailDao.insert(goalDetail);
		if (category != null) {
			goalDetail.setCategoryIdPath(category.getCategoryIdPath());
			goalDetail.setCategoryNamePath(category.getCategoryNamePath());
//			if (category.isLeaf()) {
//				goalCategoryDao.updatePercent(category.getId());
//			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#update(java.lang.Object)
	 */
	public void update(Object entity) {
		GoalDetail goalDetail = (GoalDetail) entity;
		GoalCategory category = (GoalCategory) goalCategoryDao.get(goalDetail
				.getCategoryId());
		goalDetailDao.update(goalDetail);
		if (category != null) {
			goalDetail.setCategoryIdPath(category.getCategoryIdPath());
			goalDetail.setCategoryNamePath(category.getCategoryNamePath());
//			if (category.isLeaf()) {
//				goalCategoryDao.updatePercent(category.getId());
//			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.base.BaseService#get(java.lang.Long)
	 */
	public Object get(Long id) {
		return (GoalDetail) goalDetailDao.get(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ez.supervise.service.GoalDetailService#findAll()
	 */
	public List findAll() {
		return goalDetailDao.findAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ez.supervise.service.GoalDetailService#findGoalDetailByPage(com.ez
	 * .supervise.core.Page)
	 */
	public void findGoalDetailByPage(Page page) {
		page.setRoot(goalDetailDao.findGoalDetailByPage(page));
		page.setTotalProperty(goalDetailDao.findGoalDetailByCount(page));
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ez.supervise.service.GoalDetailService#queryGoalDetailByPage(com.ez
	 * .supervise.core.Page)
	 */
	public void queryGoalDetailByPage(Page page) {
		page.setRoot(goalDetailDao.queryGoalDetailByPage(page));
		page.setTotalProperty(goalDetailDao.queryGoalDetailByCount(page));
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ez.supervise.service.GoalDetailService#findGoalByCategroy(java.lang
	 * .Long)
	 */
	public List<GoalDetail> findGoalByCategroy(Long categoryId) {
		return goalDetailDao.findGoalByCategroy(categoryId);
	}

	public void audit(List goalIdList, String status) {
		for (Object object : goalIdList) {
			if (object instanceof Long) {
				GoalDetail goalDetail = new GoalDetail();
				goalDetail.setId((Long) object);
				goalDetail.setStatus(status);
				goalDetailDao.auditGoalDetail(goalDetail);
			}
		}
	}

	public void findGoalDetailApplyByPage(Page page) {
		page.setRoot(goalDetailDao.findGoalDetailApplyByPage(page));
		page.setTotalProperty(goalDetailDao.findGoalDetailApplyByCount(page));
	}
}
